package com.bang.study;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Bang
 * @ClassName: SocXCOrg
 * @Desc: TODO
 * @Date: 2023/12/04 15:08
 * @Version: 1.0
 * @Modified By:
 */
public class SocXCOrg {

    public static void main(String[] args) {
        String string = FileUtil.readString(new File("D:\\study\\java_foundation\\parent\\udp\\src\\main\\resources\\data.json"), Charset.defaultCharset());
        JSONObject object = JSON.parseObject(string);
        JSONArray list = object.getJSONArray("list");
        JSONObject jsb = (JSONObject) list.get(0);
        JSONArray children = jsb.getJSONArray("children");
        for (Object ele : children) {
            JSONObject child = (JSONObject) ele;
            /**
             * {
             *           "id": "9469906696265729",
             *           "pid": "2",
             *           "name": "1",
             *           "assets": [
             *             {
             *               "guid": "d50f7116f8914752a90df4ab662592a3",
             *               "ip": "192.168.18.112",
             *               "level": "1",
             *               "location": "4601",
             *               "snoUnicode": "RuijieSwitch",
             *               "typeUnicode": null,
             *               "connected": true,
             *               "lastModified": 1701673560550,
             *               "depId": "9469906696265729",
             *               "describe": null
             *             }
             *           ],
             *           "status": 1,
             *           "children": [],
             *           "root": false
             *         }
             */
            List<JSONObject> childrenList = new ArrayList<>();
            for (int i = 0; i < 16; i++) {
                JSONObject ch = new JSONObject();
                ch.put("id", IdUtil.getSnowflake(1).nextIdStr());
                ch.put("pid", child.get("id"));
                ch.put("name", RandomUtil.randomString(7));
                String s = "{\"connected\": true, \"level\": \"1\", \"ip\": \"192.168.18.112\", \"guid\": \"d50f7116f8914752a90df4ab662592a3\", \"location\": \"4601\", \"depId\": \"9469906696265729\", \"lastModified\": 1701673560550, \"snoUnicode\": \"RuijieSwitch\"}";
                JSONObject jsonObject = JSON.parseObject(s);
                ArrayList<Object> objects = new ArrayList<>();
                objects.add(jsonObject);
                ch.put("assets", objects);
                ch.put("status", RandomUtil.randomInt(1,4));
                ch.put("root", false);
                ch.put("children", Collections.emptyList());
                ch.put("order",i+1);
                childrenList.add(ch);
            }
            child.put("children", childrenList);

            child.put("name",RandomUtil.randomString(13));
            child.put("status",4);
        }

        FileUtil.writeString(object.toJSONString(),new File("D:\\study\\java_foundation\\parent\\udp\\src\\main\\resources\\dst.json"),Charset.defaultCharset());
    }
}
